package aula_io_stream;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;

public class TesteEntrada1 {

    public static void main(String[] args) throws Exception {

        ArrayList<String> lista = new ArrayList<String>();

        BufferedReader teclado = new BufferedReader(new InputStreamReader(System.in));
        try {
            BufferedReader arquivoin = new BufferedReader(new InputStreamReader(new FileInputStream("E:\\Codigos\\Java\\JavaPos\\src\\aula_io_stream\\lista.txt")));
            try {                                                                              //E:\Codigos\Java\JavaPos\src\aula_io_stream     
                String x;
                while ((x = arquivoin.readLine()) != null) {
                    lista.add(x);
                }
            } finally {
                arquivoin.close();
            }
        } catch (Exception e) {
        }

        while (true) {
            for (String x : lista) {
                System.out.println(x);
            }

            System.out.println("Nova palavra (Fim para terminar):");
            String y = teclado.readLine();
            if (y.equalsIgnoreCase("Fim")) {
                break;
            }
            lista.add(y);
        }
        PrintWriter arquivoOut = new PrintWriter(new OutputStreamWriter(new FileOutputStream("E:\\Codigos\\Java\\JavaPos\\src\\aula_io_stream\\lista.txt")));
        
        for(String x:lista){
            arquivoOut.println(x);
            arquivoOut.flush();
        }
        arquivoOut.close();

     
    }
}
